package com.sun.book.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.sun.book.R;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

public class IconsDb {

	public final static String iconsDb = "brower";
	private SQLiteDatabase brower;

	public void inintDB(Context context) {
		try {
			String dataBasePath = Environment.getExternalStorageDirectory()
					.getAbsolutePath();
			String dataBasefileName = dataBasePath + "/BrowerDb" + "/"
					+ iconsDb;
			File fileDb = new File(dataBasePath + "/BrowerDb");
			if (!fileDb.exists()) {
				fileDb.mkdir();
			}
			if (!new File(dataBasefileName).exists()) {

				InputStream is = context.getResources().openRawResource(
						R.raw.brower);
				FileOutputStream os = new FileOutputStream(dataBasefileName);
				byte[] buf = new byte[1024];
				int count = 0;
				while ((count = is.read(buf)) > 0) {
					os.write(buf, 0, count);
				}
				os.flush();
				os.close();
				is.close();
			}
			brower = SQLiteDatabase
					.openOrCreateDatabase(dataBasefileName, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String[] getmHpPressIcon() {
		String[] mHpPressIcon;
		int i = 0;
		String sql = "select * from icons";
		if (brower == null) {
			return null;
		}
		Cursor cursor = brower.rawQuery(sql, null);
		mHpPressIcon = new String[cursor.getCount()];
		while (cursor.moveToNext()) {
			String name = cursor.getString(1);
			String url = cursor.getString(2);
			String icons = cursor.getString(3);
			mHpPressIcon[i] = icons;
			i++;
		}
		cursor.close();
		return mHpPressIcon;
	}

	public void close() {
		brower.close();	 
		
	}

	public void delete(int i) {
		String sql = "delete  from icons where _id like "+i+";";
		Cursor cursor = brower.rawQuery(sql, null);
		while(cursor.moveToNext()){
			System.out.println("SSSSSSS");
		}
		cursor.close();
	}

}
